/**
    @project {
        "templateId": "org.helgoboss.custom",
        "version": "0.1-SNAPSHOT",
        "dependencies": [
            "org.scala-lang:scala-library:2.9.0-1",
            "org.helgoboss:task:0.1-SNAPSHOT",
            "org.helgoboss:scala-additions:0.1-SNAPSHOT",
            "org.helgoboss:osgi-additions:0.1-SNAPSHOT",
            "org.helgoboss:app-tray:0.1-SNAPSHOT",
            "org.helgoboss:user-logger:0.1-SNAPSHOT"
        ]
    }
*/

package org.helgoboss.task_impl

import org.helgoboss.osgi_additions._
import org.helgoboss.task.{Task, ExecuteAsTask}
import java.awt.TrayIcon
import org.helgoboss.app_tray.AppTray
import org.helgoboss.user_logger.UseUserLogger

class Activator extends OsgiProvider with OsgiServiceWatcher {
    whenBundleActive {
        whenServicesPresent[AppTray, UseUserLogger] { (appTray, useUserLogger) =>
            new ExecuteAsTask {
                def apply[R](name: String)(f: (Task) => R) = {
                    useUserLogger { userLoggerStream =>
                        val task = new Task {
                            def logStream = userLoggerStream
                            def progress_=(progress: Int) {
                                appTray.icon.displayMessage(progress + "%", name, TrayIcon.MessageType.INFO)
                            }
                        }
                        appTray.icon.displayMessage("Task started", name, TrayIcon.MessageType.INFO)
                        val result = f(task)
                        appTray.icon.displayMessage("Task finished", name, TrayIcon.MessageType.INFO)
                        result
                    }
                }
            }.providesService[ExecuteAsTask]
        }
    }

}


